/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年4月10日 下午9:26:53
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pc.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.domain.po.UserCartPo;
import com.itttq.gshop.domain.po.UserInfoPo;
import com.itttq.gshop.domain.po.WareCategoryPo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.connector.IUserCartConnector;
import com.itttq.gshop.generic.connector.IWareCategoryConnector;
import com.itttq.gshop.generic.controller.GenericController;
import com.itttq.gshop.generic.util.Constants;

/**   
 * @ClassName:  GenericController
 * @author: 迷蝶
 * @date:   2018年4月10日 下午9:26:53 
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class GenericPcController extends GenericController{
	private static final long serialVersionUID = -5161558789460042772L;
	@Autowired
	private IUserCartConnector userCartConnector;
	@Autowired
	private IWareCategoryConnector catConnector;
	// 以下缓存暂时先缓存到内存，后续加入redis
	/** 分类MAP：key为父类ID，值为分类集合。为0表示一级分类，暂定为三级.由于Freemarker的map的key值只支持string */
	public static Map<String, List<WareCategoryPo>> catsMap = new HashMap<String, List<WareCategoryPo>>();
	/**
	 * 获取一些页头通用信息：用户信息、购物车信息等
	 * 
	 * @param request
	 * @param mv
	 * @Author 迷蝶
	 * @return void
	 */
	public void getCommon(HttpServletRequest request, ModelAndView mv) {
		UserInfoPo user = this.getUserFromSession(request);
		if(null != user) {
			mv.addObject("user", user);
			// 获取用户购物车信息，TODO GXG 从缓存获取
			UserCartPo userCart = new UserCartPo();
			userCart.setUserId(user.getId());
			ResultPageTo<UserCartPo> carts = userCartConnector.getUserCarts(userCart);
			// 购物车
			mv.addObject("carts", carts.getData());
			// 购物车商品总数
			mv.addObject("cartTotal", carts.getTotal());
		}
	}
	
	/**
	 * 初始化商品分类大缓存中
	 * 
	 * @Author 迷蝶
	 * @return void
	 */
	public Map<String, List<WareCategoryPo>> getCats() {
		if(catsMap.isEmpty()) {
			// 获取分类，三级.
			WareCategoryPo wareCategory = new WareCategoryPo();
			wareCategory.setStatus(Constants.STATUTS_OPEN);
			wareCategory.setSize(9999);
			List<WareCategoryPo> cats = catConnector.getWareCategorys(wareCategory);
			for(WareCategoryPo cat : cats) {
				List<WareCategoryPo> mapCats = catsMap.get(String.valueOf(cat.getParentId()));
				if(null == mapCats) {
					mapCats = new ArrayList<WareCategoryPo>();
				}
				mapCats.add(cat);
				catsMap.put(String.valueOf(cat.getParentId()), mapCats);
			}
		}
		return catsMap;
	}
}
